package com.shark.demo;

import java.util.*;

/**
 * @ClassName NotSafeDemo
 * @Description TODO
 * @Author 东北彭于晏
 * @Date 2024/9/2821:20
 * @Version 1.0
 **/
public class NotSafeDemo {
    public static void main(String[] args) {

        //线程不安全
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 3));
                System.out.println(list);
            }).start();
        }


        //安全
        List<String> list1 = Collections.synchronizedList(new ArrayList<>());

        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                list1.add(UUID.randomUUID().toString().substring(0, 3));
                System.out.println(list1);
            }).start();
        }

    }
}
